// 处理错误的函数
exports.getError = (err = "server internal error", errCode = 500) => {
    return {
        code: errCode,
        message: err,
        data: null
    }
}

// 处理返回结果的函数
exports.getResult = (result) => {
    return {
        code: 200,
        message: "",
        data: result
    }
}

/**
 * 所有的api都用这个处理函数包着,
 * @param {函数} handle 
 * @returns 一个异步函数
 */
exports.asyncHandle = (handle) => {
    return async (req, res, next) => {
        try {
            const result = await handle(req, res, next);
            res.send(exports.getResult(result));
        } catch (err) {
            // console.log(err, "错误消息");
            exports.getError();
        }
    }
}